/**   
 * @Title: ParamDataAction.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.web.struts2.action.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.dmx.fw.bean.mapping.pojo.Operator;
import com.dmx.fw.bean.mapping.pojo.ParameterData;
import com.dmx.fw.bean.mapping.pojo.ParameterDescription;
import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.service.interfaces.ParamDataService;
import com.dmx.fw.web.struts2.action.BaseAction;
import com.dmx.fw.web.util.Helper;
import com.opensymphony.xwork2.ActionContext;


/** 
 * @Description: 某一数据字典项增、删、改值Action
 * @author: yuzhixin
 * @date: 2010.3
 * @version V1.0 
 * 
 */
public class ParamDataAction extends BaseAction{
	private static Logger log = Logger.getLogger(ParamDataAction.class);
	private static final long serialVersionUID = -5317300620043728875L;

	private ParamDataService paramDataService;
	
	/** 
	 * @Fields paramData
	 */
	private ParameterData paramData;
	
	/** 
	 * @Fields paramDataId
	 */
	private String paramDataId;
	
	/** 
	 * @Fields q_typeCode: 要操作的字典项类型
	 */
	private String q_typeCode;
	
	/** 
	 * @Fields dataDesc: 字典项描述
	 */
	private String dataDesc;
	
	/** 
	 * @Fields q_canModify: 是否能修改标志
	 */
	private String q_canModify;
	
	/** 
	 * @Fields parmDesc: 字典项
	 */
	private ParameterDescription parmDesc;
	
	/** 
	 * @Fields superAdminFlag: 超级管理员的标识，当前管理员是否是超级管理员
	 */
	private String superAdminFlag = "N";
	private List listStatus = new ArrayList();
	
	public String execute() throws Exception {
		if (q_typeCode!=null && !"".equals(q_typeCode)){
			this.parmDesc = paramDataService.getParmDescByCode(q_typeCode);
			if (parmDesc.getModifying()){
				this.q_canModify = "1";
			}else{
				this.q_canModify = "0";
			}
		}
		return super.execute();
	}
	
	public ParamDataAction(){
		this.act = "editInput";
	}
	
	/**
     * 进入添加输入界面
     * @return String
     */
	public String doAddInput(){		
		addList();		
		if (paramData==null){
			paramData = new ParameterData();
		}
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		Operator optr = (Operator) session
				.get(ViewConstant.OPERATOR_SESSION_KEY);
		return SUCCESS;
	}
	
	/**
     * 执行添加提交操作
     * @return String
     */
	public String doAddSubmit() throws Exception{
		String retMsg = this.savaParamData();
		if (retMsg == null){
			//flushCache(paramData.getTypeCode());
			String resultUrl ="system/paramDataSearch.action?act=reQuery&busiId=searchParamData";
			this.setSuccessinfo(this.getText("info.addSuccess"));
			this.setAjaxJsonUrlResult(resultUrl);
			
		} else{
			this.setAjaxJsonMsgResult(retMsg,null);
			//return VALIDATE_ERROR_RESULT;
		}
		return null;    
	}
	
     /**
      * 进入修改输入界面
      * @return String
      */
	public String doEditInput(){
		addList();
		paramData = paramDataService.getParamData(paramDataId);
		return SUCCESS;
	}
	
    /**
     * 执行修改提交界面
     * @return String
     */
	public String doEditSubmit() throws Exception{
		String retMsg = this.updateParamData();
		if (retMsg == null){
			//flushCache(paramData.getCode());
			String resultUrl = "system/paramDataSearch.action?act=reQuery&busiId=searchParamData";
			this.setSuccessinfo(this.getText("info.modifySuccess"));
			
			this.setAjaxJsonUrlResult(resultUrl);
			//return null;
			//return SUCCESS_RESULT;	
		}
		else{
			this.setAjaxJsonMsgResult(retMsg,null);
			//return this.VALIDATE_ERROR_RESULT;
			
		}
		return null;
	}
	
     /**
      * 执行删除操作
      * @return String
      */
	public String doDelete() throws IOException{
		String resultUrl = "system/paramDataSearch.action?act=reQuery&busiId=searchParamData";
			String typeCode = paramDataService.getParamData(this.paramDataId).getTypeCode();
			paramDataService.delete(this.paramDataId);		
			paramDataService.flushCache(typeCode);
			
			this.setSuccessinfo(this.getText("info.deleteSuccess"));
			
			this.setAjaxJsonUrlResult(resultUrl);
			return null;
			
	}
	
	/**
     * 执行查看动作
     * @return String
     */
	public String doView(){
		addList();
		
		paramData = paramDataService.getParamData(paramDataId);
		return SUCCESS;
	}
	
	/**
	 * 添加字典项
	 * @return
	 */
	private String savaParamData() {

		if (paramDataService.uniqueCheck(paramData.getTypeCode(), paramData.getCode(), paramData.getName())){
			ParameterData param = new ParameterData();
			
			param.setCode(paramData.getCode());
			param.setName(paramData.getName());
			param.setSortNum(paramData.getSortNum()==null?0:paramData.getSortNum());
			param.setTypeCode(this.q_typeCode);
			param.setStatus(paramData.getStatus());
			param.setCreatedTime(new Date());
			param.setExtFld1(paramData.getExtFld1());
			param.setExtFld2(paramData.getExtFld2());
			Long paramId = paramDataService.create(param);
			paramDataService.flushCache(param.getTypeCode());
			return null;
		}
		else{
			//this.setErrorInfo(getText("message.Conflict"));
			//return false;
			return getText("message.Conflict");
		}
	}
	
	/**
	 * 修改字典项
	 * @return
	 */
	private String updateParamData() {
		//try{
			if (paramDataService.nameUniqueCheck(paramData.getTypeCode(), paramData.getName(),paramData.getParmDataId())){
				ParameterData pd=paramDataService.getParamData(paramData.getParmDataId().toString());
				pd.setStatus(paramData.getStatus());
				pd.setName(paramData.getName());
				pd.setCode(paramData.getCode());
				pd.setSortNum(paramData.getSortNum()==null?0:paramData.getSortNum());
				pd.setExtFld1(paramData.getExtFld1());
				pd.setExtFld2(paramData.getExtFld2());
				paramDataService.update(pd);
				paramDataService.flushCache(pd.getTypeCode());
				//return true;
				return null;
			}
			else{
				//this.setErrorInfo(getText("message.Conflict"));
				//return false;
				return getText("message.Conflict");
			}
		/*}
		catch(Exception e){
			this.setErrorInfo(getText("message.Error"));
			return false;	
		}*/
	}
	
	private void addList(){
		List list1 = new ArrayList();
		list1.add("1");
		list1.add(getText("parmData.valid"));
		
		List list2 = new ArrayList();
		list2.add("0");
		list2.add(getText("parmData.invalid"));
		
		listStatus.add(list1);
		listStatus.add(list2);
	}
	public String getParamDataId() {
		return paramDataId;
	}

	public void setParamDataId(String paramDataId) {
		this.paramDataId = paramDataId;
	}

	public ParamDataService getParamDataService() {
		return paramDataService;
	}

	public void setParamDataService(ParamDataService paramDataService) {
		this.paramDataService = paramDataService;
	}
	
	public ParameterData getParamData() {
		return paramData;
	}

	public void setParamData(ParameterData paramData) {
		this.paramData = paramData;
	}
	public List getListStatus() {
		return listStatus;
	}
	public void setListStatus(List listStatus) {
		this.listStatus = listStatus;
	}
	public String getQ_typeCode() {
		return q_typeCode;
	}
	public void setQ_typeCode(String code) {
		q_typeCode = code;
	}
	public String getQ_canModify() {
		return q_canModify;
	}
	public void setQ_canModify(String modify) {
		q_canModify = modify;
	}
	public String getDataDesc() {
		List paramList = new ArrayList();
		paramList.add(q_typeCode);
		List dataList = Helper.getCodeList("PARM_DESC",paramList);
		if (dataList!=null && dataList.size()>0){
			dataDesc = (String)((List)(dataList.get(0))).get(1);
			
		}
		return dataDesc;
	}
	public void setDataDesc(String dataDesc) {
		this.dataDesc = dataDesc;
	}
	public String getSuperAdminFlag() {
		this.superAdminFlag = "N";
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		Operator optr = (Operator) session
				.get(ViewConstant.OPERATOR_SESSION_KEY);
		if ("superadmin".equals(optr.getOperatorType())){
			this.superAdminFlag = "Y";
		}
		return this.superAdminFlag;
	}
	

	public ParameterDescription getParmDesc() {
		return parmDesc;
	}

	public void setParmDesc(ParameterDescription parmDesc) {
		this.parmDesc = parmDesc;
	}

}
